<?php
/**
 * @version : 1.0
 * @package : lib/struct
 * @name :  CString
 * @todo:  Lớp  cơ sở. Thao tác với chuỗi (string)
*/
class CString
{
	public $value = 0;
	static $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img>';

	/**
	 * Disallow these attributes/prefix within a tag
	 */
	static $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
	function __construct($value)
	{
		$this->value = $value;
	}
/**
 * @return string
 * @param string
 * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
 */
	function removeEvilTags()
	{
	   $source = strip_tags($this->value, CString::$allowedTags);
	   return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
	}
	
	function js_string()
	{
		return str_replace(array("'",'"',"\n"), array('&lsquo;','&quot;', '\\n'), $this->value);
	}

	function json()
	{
		return json_encode($this->value);
	}
	function lining()
	{
		$lines = split("\n",$this->value);
		$value = '<ol>';
		foreach($lines as $i => $line)
		{
			$value .= '<li>'.str_replace("\r",'',$line)."</li>";
		}
		return $value.'</ol>';
	}
	
	function explode($separator, $index){
		$array_result= explode($separator, $this->value);
		return $array_result[$index];
	}
	
	function get_image_name(){
		$pattern = explode('/', $this->value);
		$count = count($pattern);
		return $pattern[$count-1];
	}
	
	function get_last_name($separator){
		$pattern = explode($separator, $this->value);
		$count = count($pattern);
		return $pattern[$count-1];
	}
	
	function search_keyword()
	{
		$search_hightlight = array();
		//Dau vao la mot xau da duoc chuan hoa
		$st=$this->value;
		$st=strtr($st,"Ã ","ÃÃ");
	
		$st=strtr($st," ","+");
		$st=strtr($st,"|"," ");
		$count=0;
		$result="";
		for($i=0;$i<strlen($st);$i++)
		{
			if($st[$i]=='"')
			{
				$count++;
				if($count%2)$result.='+';
			}
			$result.=$st[$i];
		}
		$st=$result;
		while(preg_match('/"([^\"]*)"/',$st,$a))
		{
			$search_hightlight[]=strtr($a[1],array("+"=>"\s+","ÃÃ"=>"Ã "));
			$st=strtr($st,array($a[0]=>" "));
			$result=strtr($result,array($a[0]=>strtr($a[0],array("+"=>" "))));
		}
		//tach tu
		while(preg_match('/[^+\s-]+/',$st,$a))
		{
			$search_hightlight[]=strtr($a[0],array("ÃÃ"=>"Ã "));
			$st=strtr($st,array($a[0]=>" "));
		}
		$result=preg_replace("/([+|])/"," \\1",$result);
		if($result[0]!='-')$result="+".$result;
		$this->high_light = $search_hightlight;
		$this->value = strtr($result,array("ÃÃ"=>"Ã "));
		return $this->value;
	}
	function hightlight_keyword($keywords)
	{
		if($keywords)
		{
			$value = strtolower($keywords);
			$this->value = preg_replace(array('/^('.$value.')([^\w])/i','/([^\w])('.$value.')([^\w])/i'), array("<strong><span style='background-color:yellow;color:black'>\\1</span></strong>\\2","\\1<strong><span style='background-color:yellow;color:black'>\\2</span></strong>\\3"), $this->value);
		}
		return $this->value;
	}
	function get_tags()
	{
		require_once 'lib/utils/CVNCode.php';
		$keywords = strtolower(strip_tags($this->value));
		$keywords .= ' '.CVNCode::convert_utf8_to_latin($keywords);
		return CString($keywords)->extend_search_keyword();
	}
	function extend_search_keyword()
	{
		$keywords = strtolower(strip_tags($this->value));
		if($words = preg_split('/([^\w]+)/',$keywords,-1,PREG_SPLIT_DELIM_CAPTURE))
		{
			$keywords = '';
			foreach($words as $i => $word)
			{
				$keywords .= $word;
				if($i%2==0 and $word)
				{
					$end_char = $word{strlen($word)-1};
					if(strlen($word) < 4 and (is_numeric($end_char) or 
						(ord($end_char)>=ord('a') and ord($end_char)<=ord('z')) or $end_char=='_'))
					{
						$keywords .= 'kjfs';
					}
				}
			}
		}
		$this->value = $keywords;
		$this->highlight = array();
		$this->search_keyword();
		return $this->value;
	}
	// Khoanb Edit 26.05.2009 - Increment Params $sept(symbol Separator - Default ...)
	//mb_substr(strip_tags($item[5]), 0, 20, "utf-8")
	function trim($max = 0, $sept = '...'){
		if($max){
			if(function_exists('mb_strimwidth')){
				if(mb_strlen($this->value)>$max){
					return mb_substr(strip_tags($this->value), 0, $max, "utf-8").$sept;
					//return mb_strimwidth(trim($this->value), 0, $max,"utf-8").$sept;
				}
			}else{
				if(strlen($this->value)>$max){
					return substr(trim($this->value), 0, $max).$sept;
				}
			}
		}
		return trim($this->value);
	}
	
	static function array_merge($array1, $array2)
	{
		reset ($array2);
		while(key($array2)!==null) {
			if (is_array(current($array2)) && isset($array1[key($array2)])) {
				$array1[key($array2)] = CString::array_merge($array1[key($array2)],current($array2));
			} else {
				$array1[key($array2)] = current($array2);
			}
			next($array2);
		}
		return $array1;
	}
}
function CString($value)
{
	return new CString($value);
}
function json($st)
{
	return json_encode($st);
}
function js_string($st)
{
	return CString($st)->js_string();
}
/**
 * @return string
 * @param string
 * @desc Strip forbidden attributes from a tag
 */
function removeEvilAttributes($tagSource)
{
   return stripslashes(preg_replace('/'.CString::$stripAttrib.'/i', 'forbidden', $tagSource));
}

function debug($a){
	echo '<pre>';
	var_dump($a);
	echo '</pre>';
}
?>